package net.mksat.gan.keysmanager.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import net.mksat.gan.keysmanager.R;
import net.mksat.gan.keysmanager.activities.EntryActivity;
import net.mksat.gan.keysmanager.activities.InputDataActivity;
import net.mksat.gan.keysmanager.activities.MainPageActivity;
import net.mksat.gan.keysmanager.database.DbLoaderFragment;
import ua.edu.nuos.androidtraining2013.kms.enums.KeyStatus;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by 2 on 12.07.2014.
 */
public class KeyButtonListener implements View.OnClickListener {

    private AuditoryButton auditoryButton;
    private DbLoaderFragment dbFragment;
    Activity activity;

    public KeyButtonListener(AuditoryButton auditoryButton, Activity activity) {
        this.auditoryButton = auditoryButton;
        this.activity = activity;
    }

    @Override
    public void onClick(View view) {

        if (MainPageActivity.status) {// если ключ на вахте (статус "ПРИНЯТ") и его нужно выдать
            if (auditoryButton.getStatus().equals(KeyStatus.ACCEPTED)) { //то меняем статус ключа на "ВЫДАН"
                setStatus(MainPageActivity.status);
            } else {
                negative(MainPageActivity.status);
            }
        } else {
            if (auditoryButton.getStatus().equals(KeyStatus.TAKEN)) {
                setStatus(MainPageActivity.status);
            } else {
                negative(MainPageActivity.status);
            }
        }
    }

    private void setStatus(boolean status){
        Bundle bundle = new Bundle();
        bundle.putInt(DbLoaderFragment.ID_QUERY, DbLoaderFragment.SET_STATUS_KEY);
        bundle.putLong(DbLoaderFragment.ID_JANITOR, EntryActivity.idJanitor);
        bundle.putLong(DbLoaderFragment.ID_PERSONNEL, InputDataActivity.idPerson);
        bundle.putLong(DbLoaderFragment.ID_AUDITORIUM, auditoryButton.getId());
        if(status){
            bundle.putString(DbLoaderFragment.ID_STATUS, KeyStatus.TAKEN.name());
        } else {
            bundle.putString(DbLoaderFragment.ID_STATUS, KeyStatus.ACCEPTED.name());
        }
        activity.getFragmentManager().beginTransaction().
                add(DbLoaderFragment.newInstance(bundle), "dbLoader").commit();
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        if (auditoryButton.getSecurityDescription() != null) {     // если сигнализация есть, то появляется диалоговое окно с инструкцией
            builder.setTitle(R.string.key_status_changed)
                    .setMessage(((status)?activity.getString(R.string.alarm_off):activity.getString(R.string.alarm_on))
                            + auditoryButton.getName() + "!\n" + auditoryButton.getSecurityDescription())
                    .setIcon(R.drawable.ic_launcher)
                    .setCancelable(false)
                    .setNegativeButton("ОК",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                    Intent intent = new Intent(activity, MainPageActivity.class);
                                    activity.startActivity(intent);
                                }
                            }
                    );
            AlertDialog alert = builder.create();
            alert.show();
        } else {       // сигнализация отсутствует
            builder.setTitle(R.string.key_status_changed)
                    .setIcon(R.drawable.ic_launcher)
                    .setCancelable(false);
            final AlertDialog alert = builder.create();
            alert.show();
            final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    alert.dismiss();
                    timer.cancel();
                    Intent intent = new Intent(activity, MainPageActivity.class);
                    activity.startActivity(intent);
                }
            }, 2500);
        }
    }

    private void negative(boolean status){
        new AlertDialog.Builder(activity).setTitle((status)?R.string.key_is_taken:R.string.key_is_accepted)
                .setIcon(R.drawable.ic_launcher)
                .setCancelable(false)
                .setNegativeButton("ОК",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        }).create().show();

    }
}
